package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.widget.EditText;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.UserDao;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.event.CorrectRespEvent;
import com.hangzhou.xyj.model.type.ECorrectType;
import com.hangzhou.xyj.net.handler.UpdateSignatureHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.UpdateSignatureReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.widget.CommonTitleBarView.OnNavRightListener;
import com.hangzhou.xyj.utils.EventBusUtil;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.ToastUtil;


public class MyCorrectSignActivity extends BaseActivity {
    public static final int SUCCESS = 1;
    EditText mEtSign;
    Intent intent;

    @Override
    public void initTop() {
        enableTitle(true, "个性签名");
        enableRightNav(true, "确定");
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_correct_sign);
    }

    @Override
    public void initViews() {
        mEtSign = (EditText) findViewById(R.id.et_sign);
    }

    @Override
    public void initDatas() {
        intent = getIntent();
        mEtSign.setText(intent.getStringExtra("value"));
        mEtSign.setSelection(mEtSign.length());
    }

    @Override
    public void initListeners() {
        setOnNavRightListener(new OnNavRightListener() {
            @Override
            public void onNavRight() {
                String sign = mEtSign.getText().toString().trim();
                if (!PublicUtil.checkStrLength(sign, 15)) {
                    ToastUtil.show("签名长度必须最多15个字，请检查！");
                    return;
                }
                updateSign(sign);
            }
        });
    }

    public void updateSign(final String sign) {
        UpdateSignatureReq updateSignatureReq = new UpdateSignatureReq(Integer.valueOf(CommonPreferences.getUserId(mContext)), sign);
        HttpLoader.getInstance().sendRequest(new UpdateSignatureHandler(updateSignatureReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if ((Boolean) model) {
                    //更新应用中的信息
                    XYJApplication.getUserInfo().setSignature(sign);
                    //更新本地数据库中的信息
                    UserDao dao = new UserDao(mContext);
                    dao.addUser(XYJApplication.getUserInfo());
                    //提示成功
                    ToastUtil.show("保存成功！");
                    CorrectRespEvent event = new CorrectRespEvent();
                    event.setValue(sign);
                    event.setTag(ECorrectType.SIGN.getValue());
                    EventBusUtil.postEvent(event);
                    closeActivity();
                }
            }
        }, 0));
    }
}
